package com.bong.panpan.bongcharttools.util.csvutil;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.github.mjdev.libaums.UsbMassStorageDevice;
import com.github.mjdev.libaums.fs.UsbFile;
import com.github.mjdev.libaums.fs.UsbFileOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by hackill on 2018/1/8.
 */

public class FileUtils {

    private static final String TAG = "FileUtils";
    private static final String SUFFIX = ",";
    private final static SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    /**
     * 增加记录
     *
     * @param filePath
     * @param entityList
     * @return
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static synchronized boolean addRecord(String filePath, List<RecordEntity> entityList) throws IOException {

        if (TextUtils.isEmpty(filePath)) {
            throw new FileNotFoundException();
        }

        String dirPath = filePath.substring(0, filePath.lastIndexOf(File.separator));

        File dir = new File(dirPath);

        if (!dir.exists() && dir.mkdirs()) {
            Log.i(TAG, "addRecord: 创建文件夹");
        }

        if (entityList == null || entityList.size() == 0) return true;

        File file = new File(filePath);

        if (!file.exists() && file.createNewFile()) {
            Log.d(TAG, "addRecord: 文件新创建");
        } else {
            Log.d(TAG, "addRecord: 文件已存在");
        }
        StringBuilder stringBuilder = new StringBuilder();

        for (RecordEntity entity : entityList) {
            stringBuilder.append(dataFormat.format(entity.getTime())).append(SUFFIX);
            stringBuilder.append(String.format("%.3f",entity.getTemp())).append(SUFFIX);
            stringBuilder.append(entity.getAct()).append("\n");
        }

        FileOutputStream fos = new FileOutputStream(file, true);
        fos.write(stringBuilder.toString().getBytes());
        fos.flush();
        fos.close();
        return true;
    }

    /**
     * 是否有外接U设备
     *
     * @param context
     * @return
     */
    public static boolean isExistUsbDevice(Context context) {
        return UsbMassStorageDevice.getMassStorageDevices(context).length > 0;
    }

    /**
     * 检测SD卡是否存在
     */
    public static boolean isExistSDcard() {
        return Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
    }

    /**
     * 获取sd卡根目录
     *
     * @return
     */
    public static String getSdCardPath() {
        return Environment.getExternalStorageDirectory()
                .getAbsolutePath();
    }

    /**
     * @description 把本地文件写入到U盘中
     * @author ldm
     * @time 2017/8/22 10:22
     */
    public static void saveSDFile2OTG(final File sourceFile, final UsbFile usbFile) throws IOException {
        UsbFile uFile;
        FileInputStream fis;
        fis = new FileInputStream(sourceFile);//读取选择的文件的
        if (usbFile.isDirectory()) {//如果选择是个文件夹
            UsbFile[] usbFiles = usbFile.listFiles();
            if (usbFiles != null && usbFiles.length > 0) {
                for (UsbFile file : usbFiles) {
                    if (file.getName().equals(sourceFile.getName())) {
                        file.delete();
                    }
                }
            }
            uFile = usbFile.createFile(sourceFile.getName());
            UsbFileOutputStream uos = new UsbFileOutputStream(uFile);

            int bytesRead = 0;
            byte[] buffer = new byte[1024 * 8];
            while ((bytesRead = fis.read(buffer)) != -1) {
                uos.write(buffer, 0, bytesRead);
            }
            uos.flush();
            uos.close();
            fis.close();
        }
    }
}
